<?php
namespace app\api\controller;
use app\api\model\LoginModel;
use app\common\controller\Controller;
use app\common\utils\Redis;

// 
class Common extends Controller
{

    public function _initialize(){

    }


    // 获取当前登录用户 id
    protected function checkLoginUser($need_login = false, $session_id = null,$save_type=1)
    {

        return 74;
        if($save_type == 1)
        {
            //保存 在 session
            $info = $this->getLoginUserInSession();
        }else if($save_type ==2)
        {
            //保存 在 Redis
            $info = $this->getLoginUserInRedis($session_id);
        }


        if ($need_login && empty($info)) {
            to_json_encode(300, '请登录', array('status' => false));
        }

        return isset($info['user_id'])?$info['user_id']:false;
    }

    //微信公众号 获取 当前登录用户
    private function getLoginUserInSession()
    {
        return session('user');
    }
    //微信小程序 获取 当前登录用户
    private function getLoginUserInRedis($session_id=null)
    {
        if ($session_id === null) {
            $param_name = 'HTTP_UNIQUE_STR'; //自定义参数默认会加上HTTP_前缀，且大写
            $session_id = $_SERVER[$param_name];
        }
        if (!$session_id) {

           return false;
        }
        
        $session_3rd = $this->getSessionkeyBysessionId($session_id);
        if (empty($session_3rd)) {
            return false;
        } 
        
        $expl = explode('@@', $session_3rd);
        $openid = $expl[1];

        return LoginModel::getUserInfoByWxOpenid($openid);

    }
    // 小程序 登录 返回 session_key  openid
    public function WXLoginOpenId($code){

        $appid = 'wx6fc895932c526bc2';
        $secret = '06f600cec204bdeaeadc82d654c3c5fc';
        

        //  请求 
        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code';

        $url = sprintf($url, $appid, $secret, $code);

        $ch = curl_init();
		//设置超时
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		//运行curl，结果以json形式返回
        $res = curl_exec($ch);
        curl_close($ch);

        $data = json_decode($res, true);

        return $data;
    }
    
    // 小程序生成 3rd_session
    public function session_3rd($len = 16){
        $fp = @fopen('/dev/urandom', 'rb');

        $result = '';

        if ($fp !== false) {

            $result .= @fread($fp, $len);

            @fclose($fp);

        } else {

            trigger_error('Can not open /dev/urandom.');

        }

// convert from binary to string

        $result = base64_encode($result);

// remove none url chars

        $result = strtr($result, '+/', '-_');

        return substr($result, 0, $len);

    }

    //通过  session_id 获取 session_key  和 openid

    public function getSessionkeyBysessionId($session_id)
    {
         $redis= Redis::instance();

         return  $redis->get($session_id);
         
    }
}
